package com.business.api.official.toursism.websitecomment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.business.api.official.toursism.websitecomment.dto.WebsiteCommentApiDTO;
import com.business.api.official.toursism.websitecomment.from.WebsiteCommentApiSaveFrom;
import com.business.api.official.toursism.websitecomment.from.WebsiteCommentApiUpdateFrom;
import com.business.api.official.toursism.websitecomment.manager.WebsiteCommentApiManager;
import com.business.api.official.toursism.websitecomment.query.WebsiteCommentApiPageQuery;
import com.business.api.official.toursism.websitecomment.query.WebsiteCommentApiQuery;
import com.business.api.official.toursism.websitecomment.service.WebsiteCommentApiService;
import com.business.api.official.toursism.websitecomment.vo.WebsiteCommentApiBriefPageVO;
import com.business.api.official.toursism.websitecomment.vo.WebsiteCommentApiDetailListVO;
import com.business.api.official.toursism.websitecomment.vo.WebsiteCommentApiDetailPageVO;
import com.business.api.official.toursism.websitecomment.vo.data.WebsiteCommentApiBriefData;
import com.business.api.official.toursism.websitecomment.vo.data.WebsiteCommentApiDetailData;
import com.business.system.official.toursism.websitecomment.entity.WebsiteComment;
import com.business.system.official.toursism.websitecomment.service.WebsiteCommentService;
import com.seed.core.exception.ManagerException;
import com.seed.core.exception.ServiceException;
import com.seed.core.pojo.dto.SeedMessageDTO;
import com.seed.core.pojo.service.impl.SeedApiServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @description 评论
 * @author CodeGenerator
 * @date   2025-01-25
 * @version V1.0
 */
@Slf4j
@Service("websiteCommentApiService")
@Transactional(rollbackFor = ServiceException.class)
public class WebsiteCommentApiServiceImpl extends SeedApiServiceImpl<WebsiteCommentApiManager, WebsiteComment> implements WebsiteCommentApiService {

    @Autowired
    private WebsiteCommentService systemService;


    /**
     * 持久化
     *
     * @param websiteCommentFrom 需要保存的对象
     * @return id，如果保存失败返回空字符串
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteCommentApiDTO save(WebsiteCommentApiSaveFrom websiteCommentFrom) throws ServiceException {
        WebsiteCommentApiDTO apiDTO = new WebsiteCommentApiDTO();
        WebsiteComment websiteComment = new WebsiteComment();
        try {
            BeanUtils.copyProperties(websiteCommentFrom, websiteComment, "id");
        } catch (Exception e) {
            throw new ServiceException(e, websiteCommentFrom);
        }
        SeedMessageDTO baseSeedDTO = this.save(websiteComment);
        if (!baseSeedDTO.getSuccess()) {
            apiDTO.setCode(baseSeedDTO.getCode());
            apiDTO.setMessage(baseSeedDTO.getMessage());
            apiDTO.setSuccess(false);
            return apiDTO;
        }
        apiDTO.setWebsiteComment(websiteComment);
        return apiDTO;
    }

    /**
     * 部分更新
     *
     * @param websiteCommentFrom 需要更新的对象
     * @return 是否成功
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteCommentApiDTO updateSelectiveById(WebsiteCommentApiUpdateFrom websiteCommentFrom) throws ServiceException {
        WebsiteCommentApiDTO apiDTO = new WebsiteCommentApiDTO();
        if (StringUtils.isEmpty(websiteCommentFrom.getId())) {
            apiDTO.setCode("A0410");
            apiDTO.setMessage("更新主键为空");
            apiDTO.setSuccess(false);
            return apiDTO;
        }
        WebsiteComment websiteComment = new WebsiteComment();
        try {
            BeanUtils.copyProperties(websiteCommentFrom, websiteComment);
        } catch (Exception e) {
            throw new ServiceException(e, websiteCommentFrom);
        }
        SeedMessageDTO baseSeedDTO = this.updateById(websiteComment);
        if (!baseSeedDTO.getSuccess()) {
            apiDTO.setCode(baseSeedDTO.getCode());
            apiDTO.setMessage(baseSeedDTO.getMessage());
            apiDTO.setSuccess(false);
            return apiDTO;
        }
        return apiDTO;
    }

    /**
     * 获取全部
     *
     * @param query 查询条件
     * @return 详情VOList
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteCommentApiDetailListVO listAll(WebsiteCommentApiQuery query) throws ServiceException {
        WebsiteComment queryEntity = new WebsiteComment();
        BeanUtils.copyProperties(query, queryEntity);
        LambdaQueryWrapper<WebsiteComment> lambdaQueryWrapper = new LambdaQueryWrapper<>(queryEntity);
        List<WebsiteComment> list = this.list(lambdaQueryWrapper);
        List<WebsiteCommentApiDetailData> detailVOList;
        try {
            detailVOList = baseManager.listConvertApiDetailData(list);
        } catch (ManagerException e) {
            throw new ServiceException(e, list);
        }
        return new WebsiteCommentApiDetailListVO(detailVOList);
    }

    /**
     * 简略对象条件查询
     *
     * @param query 查询条件
     * @return 查询出来的简略对象
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteCommentApiBriefPageVO getBriefPageVO(WebsiteCommentApiPageQuery query) throws ServiceException {
        Page<WebsiteComment> page = new Page<>(query.getQueryPage(), query.getQuerySize());
        WebsiteComment queryEntity = new WebsiteComment();
        BeanUtils.copyProperties(query, queryEntity);
        LambdaQueryWrapper<WebsiteComment> lambdaQueryWrapper = new LambdaQueryWrapper<>(queryEntity);
        IPage<WebsiteComment> pageData = this.page(page, lambdaQueryWrapper);
        List<WebsiteComment> list = pageData.getRecords();
        List<WebsiteCommentApiBriefData> voList;
        try {
            voList = baseManager.listConvertApiBriefData(list);
        } catch (ManagerException e) {
            throw new ServiceException(e, list);
        }
        return new WebsiteCommentApiBriefPageVO(page, voList);
    }

    /**
     * 详细对象条件查询
     *
     * @param query 查询条件
     * @return 查询出来的详细对象
     * @throws ServiceException 业务层异常
     */
    @Override
    public WebsiteCommentApiDetailPageVO getDetailPageVO(WebsiteCommentApiPageQuery query) throws ServiceException {
        Page<WebsiteComment> page = new Page<>(query.getQueryPage(), query.getQuerySize());
        WebsiteComment queryEntity = new WebsiteComment();
        BeanUtils.copyProperties(query, queryEntity);
        LambdaQueryWrapper<WebsiteComment> lambdaQueryWrapper = new LambdaQueryWrapper<>(queryEntity);
        IPage<WebsiteComment> pageData = this.page(page, lambdaQueryWrapper);
        List<WebsiteComment> list = pageData.getRecords();
        List<WebsiteCommentApiDetailData> voList;
        try {
            voList = baseManager.listConvertApiDetailData(list);
        } catch (ManagerException e) {
            throw new ServiceException(e, list);
        }
        return new WebsiteCommentApiDetailPageVO(page, voList);
    }

}